#include "stdafx.h"
#include "QEnvTable.h"
#include "QEnvStatus.h"

QEnvTable::QEnvTable( QWidget * parent )
	: QTableWidget(parent)
	, mCustom( NULL ) { 
	setupUi(); 
}
QEnvTable::~QEnvTable( ){ clear(); }

bool QEnvTable::hasItem( const QEnvPath& path ) const{ 
	for( auto i=mStore.begin(); i!=mStore.end(); i++ )
		if( i->get()->getPath() == path ) return true;
	return false;
}
QEnvTable::ItemHolder& QEnvTable::getItem( const QEnvPath& path ){ 
	for( auto i=mStore.begin(); i!=mStore.end(); i++ )
		if( (*i)->getPath() == path ) return *i;
	return mStore.invalidReturn();
}
const QEnvTable::ItemHolder QEnvTable::getItem( const QEnvPath& path ) const{ 
	for( auto i=mStore.begin(); i!=mStore.end(); i++ )
		if( (*i)->getPath() == path ) return *i;
	return mStore.invalidReturn();
}
void QEnvTable::setCustom( QTableWidget * w ) { mCustom = w; setupUi( w ); }

QTableWidget * QEnvTable::custom(){ return mCustom; }
const QTableWidget* QEnvTable::custom() const{ return mCustom; }
void QEnvTable::removeItem( const QEnvPath& path )
{
	removeItem( getItem(path) );
}
void QEnvTable::removeItem( ItemHolder& item )
{
	mStore.remove( item );
}
void QEnvTable::clearContents( )
{
	mStore.destroy();
	QEnvStatus::addMessage( FROMQT(tr("Clearing Table Widget")), true );
	QTableWidget::clearContents();
}
void QEnvTable::setupUi( QTableWidget* table )
{
	(table?table:this)->clearContents();
	(table?table:this)->verticalHeader()->setVisible( false );
	(table?table:this)->verticalHeader()->setStretchLastSection(false);
	(table?table:this)->horizontalHeader()->setStretchLastSection(true);

	(table?table:this)->setSelectionBehavior( QAbstractItemView::SelectRows );
	(table?table:this)->setColumnCount( 2 );
	(table?table:this)->setColumnWidth( 0, 420 );
	(table?table:this)->setRowCount( 0 );
	(table?table:this)->verticalScrollBar()->setVisible( true );
	(table?table:this)->setHorizontalHeaderLabels( QStringList() << tr("Path") << tr("Extra")  );
}

void QEnvTable::setFilter( const String &filter )
{
	if( filter.empty() )
	{
		clearFilters();
		return;
	}
	for( int i=0; i<rowCount(); i++ )
	{
		setRowHeight( i, 20 );
		String it = FROMQT( item( i, 0 )->text() );
		String it2 = filter;
		for( size_t iii = 0; iii < it.size(); iii++ ) it.data()[iii] = tolower(it.data()[iii]);
		for( size_t iii = 0; iii < it2.size(); iii++ ) it2.data()[iii] = tolower(it2.data()[iii]);
		if( it.find(it2) != std::string::npos )
			showRow( i );
		else hideRow( i );
	}
}
void QEnvTable::clearFilters( )
{
	for( int i=0; i<rowCount(); i++ )
	{
		showRow( i );
		setRowHeight( i, 20 );
	}
}
QEnvTable::ItemHolder QEnvTable::addItem( const QEnvPath& path )
{
	if( path.path().trimmed().empty() ){
		QEnvStatus::addStatus( FROMQT(tr("Found empty path %1").arg(path.path())), 0 );
		return ItemHolder();
	}
	else if( hasItem( path ) ){
		QEnvStatus::addStatus( FROMQT(tr("Found path duplicate %1").arg(path.path())) );
		return ItemHolder();
	}
	else
	{
		int id = rowCount();
		insertRow( id );

		bool rem_ = false;
		String flags = "valid";
		if( !path.exists() ){
			flags = "invalid";
			QEnvStatus::addMessage( FROMQT(tr("Path %1 does not exist, should I remove it ?").arg(path.path())), true );
			if( !QCmdLine::Instance().isQuiet() )
			{
				if( QMessageBox::question( this, tr("Empty Path Detected"), tr("Path %1 does not exist, should I remove it ?").arg(path.path()), QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )
				{ 
					rem_ = true; 
					QEnvStatus::addMessage( "\t answer: Yes", true );
				}
				else {
					rem_ = false;
					QEnvStatus::addMessage( "\t answer: No", true );
					flags += "|untouched";
				}
			}
			else
			{
				QEnvStatus::addMessage( "\tQuiet mode enabled, skipping message box with auto answer: NO", true );
				rem_=false;
				flags += "|untouched";
			}
		}
		else if( path.isDir() ) flags += "|directory";
		else if( path.isFile() ) flags += "|file";
		
		if( rem_ )
		{}
		else
		{
			QEnvTableItem * iitm= new QEnvTableItem( path, this );
			setItem( id, 0, iitm );
			setItem( id, 1, new QTableWidgetItem( flags ) );
		
			setRowHeight( id, 20 );

			QEnvStatus::addStatus( FROMQT(tr("Path %1 added").arg(path.path())), 0, true );
			//return ItemHolder();
			return mStore.store( iitm, false );
		}
	}
	return ItemHolder();
}
size_t QEnvTable::findIndex( const QEnvPath& path ) const{ 
	for( size_t i=0; i<mStore.size(); i++ )
		if( mStore.retrieve(i)->getPath() == path ) return i;
	return -1;
}
void QEnvTable::grabEnv()
{
	clearContents();
	String p(getenv( "PATH" ));
	QEnvStatus::addStatus(FROMQT(tr("Examining %1").arg(p)), 0, true);
	StringVector pathList( StringVector::split( p, ";" ) );
	
	for( auto i=pathList.begin(); i!=pathList.end(); i++ )
	{
		addItem( *i );
	}
	if( mCustom )
	{
		mCustom->clearContents();
		QString h(QString::fromUtf8(getenv("LIT_HOME")));
		QString oi(QString::fromUtf8(getenv("LIT_OGRE_INCLUDES")));
		QString ol(QString::fromUtf8(getenv("LIT_OGRE_LIBS")));
		QString old(QString::fromUtf8(getenv("LIT_OGRE_LIBS_D")));
		QString oii(QString::fromUtf8(getenv("LIT_OGRE_INPUTS")));
		QString oid(QString::fromUtf8(getenv("LIT_OGRE_INPUTS_D")));
		
		QString ni(QString::fromUtf8(getenv("LIT_NEWTON_INCLUDES")));
		QString nl(QString::fromUtf8(getenv("LIT_NEWTON_LIBS")));
		QString nld(QString::fromUtf8(getenv("LIT_NEWTON_LIBS_D")));
		QString nii(QString::fromUtf8(getenv("LIT_NEWTON_INPUTS")));
		QString nid(QString::fromUtf8(getenv("LIT_NEWTON_INPUTS_D")));

		
		mCustom->insertRow( 0 ); mCustom->setItem( 0, 0, new QTableWidgetItem("LIT_HOME") );
		mCustom->insertRow( 1 ); mCustom->setItem( 1, 0, new QTableWidgetItem("LIT_OGRE_INCLUDES") );
		mCustom->insertRow( 2 ); mCustom->setItem( 2, 0, new QTableWidgetItem("LIT_OGRE_LIBS") );
		mCustom->insertRow( 3 ); mCustom->setItem( 3, 0, new QTableWidgetItem("LIT_OGRE_LIBS_D") );
		mCustom->insertRow( 4 ); mCustom->setItem( 4, 0, new QTableWidgetItem("LIT_OGRE_INPUTS") );
		mCustom->insertRow( 5 ); mCustom->setItem( 5, 0, new QTableWidgetItem("LIT_OGRE_INPUTS_D") );

		mCustom->insertRow( 6 ); mCustom->setItem( 6, 0, new QTableWidgetItem("LIT_NEWTON_INCLUDES") );
		mCustom->insertRow( 7 ); mCustom->setItem( 7, 0, new QTableWidgetItem("LIT_NEWTON_LIBS") );
		mCustom->insertRow( 8 ); mCustom->setItem( 8, 0, new QTableWidgetItem("LIT_NEWTON_LIBS_D") );
		mCustom->insertRow( 9 ); mCustom->setItem( 9, 0, new QTableWidgetItem("LIT_NEWTON_INPUTS") );
		mCustom->insertRow( 10 ); mCustom->setItem( 10, 0, new QTableWidgetItem("LIT_NEWTON_INPUTS_D") );

		mCustom->setItem( 0, 1, new QTableWidgetItem(h) );
		mCustom->setItem( 1, 1, new QTableWidgetItem(oi) );
		mCustom->setItem( 2, 1, new QTableWidgetItem(ol) );
		mCustom->setItem( 3, 1, new QTableWidgetItem(old) );
		mCustom->setItem( 4, 1, new QTableWidgetItem(oii) );
		mCustom->setItem( 5, 1, new QTableWidgetItem(oid) );

		mCustom->setItem( 6, 1, new QTableWidgetItem(ni) );
		mCustom->setItem( 7, 1, new QTableWidgetItem(nl) );
		mCustom->setItem( 8, 1, new QTableWidgetItem(nld) );
		mCustom->setItem( 9, 1, new QTableWidgetItem(nii) );
		mCustom->setItem( 10, 1, new QTableWidgetItem(nid) );
	}

}
void QEnvTable::saveEnv()
{
	String str;
	for( int i=0; i<rowCount(); i++ )
	{
		QEnvTableItem * item = (QEnvTableItem*)cellWidget( i, 0 );
		if( item ) str += item->getPath().path() + ";";
	}
	String p("PATH=" + str);
	QEnvStatus::addStatus( p + "\r\n\t"+FROMQT(tr("returned"))+" " + QString::number(putenv(p.cstr())), 0, true );
}